﻿Imports Hinshelwood.TFSStickyBuddy.Modules.Interfaces
Imports Infragistics.Windows.Themes
Imports System.Windows.Resources
Imports System.IO


Namespace Services


    Public Class ThemeService
        Implements IThemeService

        Private m_CurrentTheme As ResourceDictionary
        Private m_ThemeData As Dictionary(Of String, ThemeItem) = New Dictionary(Of String, ThemeItem)

        Public Sub New()
            RefreshThemeData()
        End Sub

        Public Function GetRegisteredThemes() As System.Collections.Generic.List(Of ThemeItem) Implements Interfaces.IThemeService.GetRegisteredThemes
            Return (From kp In m_ThemeData Select kp.Value).ToList ' ThemeManager.GetThemes(True).ToList
        End Function

        Public Function CurrentTheme() As ThemeItem Implements Interfaces.IThemeService.CurrentTheme
            If m_ThemeData.ContainsKey(ThemeManager.CurrentTheme) Then
                Return m_ThemeData(ThemeManager.CurrentTheme)
            End If
            Return Nothing
        End Function

        Private Sub RefreshThemeData()
            Try
                Dim res As Uri = New Uri("/Hinshelwood.TFSStickyBuddy.Modules.Theme;component/Data/ThemeRegister.xml", UriKind.Relative)
                Dim sri As StreamResourceInfo = Application.GetResourceStream(res)
                Dim reader As New StreamReader(sri.Stream)
                Dim document As XDocument = XDocument.Load(reader)
                Dim var = From x In document.Descendants("ThemeItem") _
                          Select New ThemeItem With { _
                                .ThemeId = x.Attribute("themeId").Value, _
                                .Name = x.Element("Title").Value, _
                                .Description = x.Element("Description").Value, _
                                .ImageSource = GetImageUri(x.Attribute("themeId").Value) _
                                }
                m_ThemeData = var.ToDictionary(Of String)(Function(t) t.ThemeId)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

            'For Each st In ThemeManager.GetThemes(True)
            '    If Not m_ThemeData.ContainsKey(st) Then
            '        m_ThemeData.Add(st, New ThemeItem With {.ThemeId = st, .Name = st, .ImageSource = GetImageUri(st)})
            '    End If
            'Next
        End Sub

        Public Sub SetTheme(ByVal theme As ThemeItem) Implements Interfaces.IThemeService.SetTheme
            If theme Is Nothing Then
                Debug.Write(String.Format("{0} is an invalid theme", theme.ThemeId))
                Exit Sub
            End If

            If Not m_CurrentTheme Is Nothing Then
                Application.Current.Resources.MergedDictionaries.Remove(m_CurrentTheme)
            End If

            Dim resourceDictionary As ResourceDictionary = ThemeManager.GetResourceSet(theme.ThemeId, ThemeManager.AllGroupingsLiteral)
            If Not resourceDictionary Is Nothing Then
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary)
                m_CurrentTheme = resourceDictionary
                ThemeManager.CurrentTheme = theme.ThemeId
            End If
            
        End Sub

        Public Function GetThemeByName(ByVal themeId As String) As ThemeItem Implements Interfaces.IThemeService.GetThemeByName
            Return m_ThemeData(themeId)
        End Function

        Public Function GetImageUri(ByVal ThemeId As String) As Uri
            Dim mask As String = "/Hinshelwood.TFSStickyBuddy.Modules.Theme;component/Resources/Images/{0}.png"
            Dim themeImage As Uri = New Uri(String.Format(mask, ThemeId), UriKind.Relative)
            Dim sri As StreamResourceInfo
            Try
                sri = Application.GetResourceStream(themeImage)
                Return themeImage
            Catch ex As Exception
                Return New Uri(String.Format(mask, "Default"), UriKind.Relative)
            End Try
        End Function

        Private m_TempCache As ThemeItem = Nothing

        Public Sub PreviewTheme(ByVal theme As ThemeItem) Implements Interfaces.IThemeService.PreviewTheme
            If m_TempCache Is Nothing Then
                m_TempCache = GetThemeByName(ThemeManager.CurrentTheme)
                SetTheme(theme)
            Else
                SetTheme(m_TempCache)
                m_TempCache = Nothing
            End If

        End Sub

    End Class

End Namespace